package com.sjzd.meeting.controller;

import com.github.pagehelper.PageInfo;
import com.sjzd.meeting.pojo.MeetingPublish;
import com.sjzd.meeting.pojo.MeetingPublishRank;
import com.sjzd.meeting.service.IMeetingPublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/meetingPublish")
public class MeetingPublishController {

    @Autowired
    private IMeetingPublishService meetingPublishService;

    @PostMapping("/publish")
    @ResponseBody
    public String publish(MeetingPublish meetingPublish) {
        int flag = meetingPublishService.save(meetingPublish);
        if(flag > 0) {
            return "2"; // 成功
        } else {
            return "1"; // 失败
        }
    }

    /**
     *  根据uid分页查询已发布的讲座订单
     * @param uid
     * @param pageNum
     */
    @GetMapping("/published")
    public String published(@RequestParam("uid") Integer uid,
                            @RequestParam(name="pageNum",defaultValue = "1") String pageNum,
                            Model model) {
        PageInfo<MeetingPublish> pageInfo =  meetingPublishService.selectByUid(uid, Integer.parseInt(pageNum), 3);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("uid", uid);
        return "weixin/published";
    }

    /**
     * 查询以发布讲座的详细信息
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/publishedDetail")
    public String publishedDetail(String id, Model model) {
        MeetingPublish meetingPublish = meetingPublishService.selectPublishMeetingDetailById(id);
        model.addAttribute("meeting", meetingPublish);
        return "weixin/publishedDetail";
    }

    /**
     *  查询召开排行榜
     */
    @GetMapping("/publishRank")
    public String publishRank(Model model) {
       List<MeetingPublishRank> rankList =  meetingPublishService.selectPublishMeetingRank();
       model.addAttribute("rankList", rankList);
       return "weixin/publishRank";
    }
}
